package com.bw.wxy.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.wxy.R;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BasevpFragment;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.di.DaggerLREComponent;
import com.bw.wxy.mvp.di.LREModules;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ImageBannerEntity;
import com.bw.wxy.mvp.model.entity.NewsEntity;
import com.bw.wxy.mvp.presenter.LREPresenter;
import com.bw.wxy.mvp.view.news.adapter.ContentNewsAdapter;
import com.bw.wxy.mvp.view.news.adapter.ImageBannerAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment_News_Content extends BasevpFragment<LREPresenter> implements LREContract.ILREView , OnRefreshLoadMoreListener {
    private SmartRefreshLayout contentNewsSmart;
    private RecyclerView contentNewsRv;
    private View headView;
    private ViewPager headVp;
    private ImageBannerAdapter vpAdapter;
    private ContentNewsAdapter adapter;
    private long channel_id;

    public Fragment_News_Content(long channel_id){
        this.channel_id = channel_id;
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment__news__content;
    }

    @Override
    public void initView() {
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_news_banner,null);
        headVp = headView.findViewById(R.id.news_banner_vp);
        contentNewsSmart = f(R.id.content_news_smart);
        contentNewsRv = f(R.id.content_news_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        contentNewsRv.setLayoutManager(manager);
        adapter = new ContentNewsAdapter(new ArrayList<>());
        adapter.addHeaderView(headView);
        contentNewsRv.setAdapter(adapter);
        contentNewsSmart.setEnableLoadMore(true);
        contentNewsSmart.setEnableRefresh(true);
        contentNewsSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        contentNewsSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        contentNewsSmart.setOnRefreshLoadMoreListener(this);
    }

    @Override
    public void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        Map<String,Object>bannerMap = new HashMap<>();
        bannerMap.put("code",HttpCode.IMAGERBANNERCODE);
        p.requestAll(bannerMap,map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build().lreRefresh(this);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        LogUtils.json(baseEntity);
        if (baseEntity instanceof ImageBannerEntity){
            //banner列表返回
            ImageBannerEntity bannerEntity = (ImageBannerEntity) baseEntity;
            vpAdapter = new ImageBannerAdapter(bannerEntity.getValues());
            headVp.setAdapter(vpAdapter);
        }
        if (baseEntity instanceof NewsEntity){
            //新闻列表返回
            NewsEntity newsEntity = (NewsEntity) baseEntity;
            adapter.setNewData(newsEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        NewsEntity newsEntity = (NewsEntity) baseEntity;
        adapter.setNewData(newsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        Log.i("wxy", "loadMoreRecyclerView: "+"进入loadMoreRecyclerView");
        NewsEntity newsEntity = (NewsEntity) baseEntity;
        if (newsEntity.getValues()==null){
            Toast.makeText(getActivity(), "最后一页", Toast.LENGTH_SHORT).show();
            return;
        }
        if (newsEntity.getValues().size() == 0){
            Toast.makeText(getActivity(), "最后一页", Toast.LENGTH_SHORT).show();
            return;
        }
        List<NewsEntity.Values> list = adapter.getData();
        list.addAll(newsEntity.getValues());
        adapter.setNewData(list);
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        Log.i("wxy", "onLoadMore: "+"进入onLoadMore");
        refreshLayout.finishLoadMore();
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        p.requestRefresh(map);
    }
}